package com.usian.wemedia.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.usian.apis.wemedia.WmUserControllerApi;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmUser;
import com.usian.wemedia.service.WmUserService;
import com.usian.wemedia.service.impl.WmUserServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api/v1/user")
public class WmUserController implements WmUserControllerApi {
    @Autowired
    private WmUserServiceImpl service;

    @PostMapping("save")
    @Override
    public ResponseResult save(@RequestBody WmUser wmUser) {
        if (wmUser==null) return service.paramError;
        service.save(wmUser);
        return ResponseResult.okResult(wmUser);
    }

    @GetMapping("/findByName/{name}")
    @Override
    public WmUser findByName(@PathVariable("name") String name) {
        LambdaQueryWrapper<WmUser> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(name)){ //不为空的情况,根据名称查询到实体类对象
            queryWrapper.eq(WmUser::getName,name);
            WmUser wmUser = service.getOne(queryWrapper);
            return wmUser;
        }
        return null;
    }

    @GetMapping("findOne/{id}")
    @Override
    public WmUser findWmUserById(@PathVariable Long id) {
        return service.getById(id);
    }

    @PostMapping("updateAuthorId")
    public ResponseResult updateAuthorId(@RequestBody WmUser wmUser){
        service.updateById(wmUser);
        return ResponseResult.okResult(wmUser);
    }
}
